// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcBezierPatch.h"

MgcImplementRTTI(MgcBezierPatch,MgcObject);
MgcImplementStream(MgcBezierPatch);

//----------------------------------------------------------------------------
MgcBezierPatch::MgcBezierPatch (unsigned int uiDegree,
    unsigned int uiIndexQuantity, unsigned int* auiIndex)
{
    assert( uiDegree > 0 && uiIndexQuantity >= 3 && auiIndex );

    m_uiDegree = uiDegree;
    m_uiIndexQuantity = uiIndexQuantity;
    m_auiIndex = auiIndex;
}
//----------------------------------------------------------------------------
MgcBezierPatch::MgcBezierPatch ()
{
    m_uiDegree = 0;
    m_uiIndexQuantity = 0;
    m_auiIndex = 0;
}
//----------------------------------------------------------------------------
MgcBezierPatch::~MgcBezierPatch ()
{
    delete[] m_auiIndex;
}
//----------------------------------------------------------------------------
unsigned int MgcBezierPatch::GetVerticesPerTrianglePatch (
    unsigned int uiLevel)
{
    // V = (2^L+1)*(2^L+2)/2
    unsigned int uiTwoPowLPlusOne = (1 << uiLevel) + 1;
    return uiTwoPowLPlusOne*(uiTwoPowLPlusOne+1) >> 1;
}
//----------------------------------------------------------------------------
unsigned int MgcBezierPatch::GetVerticesPerRectanglePatch (
    unsigned int uiLevel)
{
    // V = (2^L+1)^2
    unsigned int uiTwoPowLPlusOne = (1 << uiLevel) + 1;
    return uiTwoPowLPlusOne*uiTwoPowLPlusOne;
}
//----------------------------------------------------------------------------
unsigned int MgcBezierPatch::GetVerticesPerCylinderPatch (
    unsigned int uiLevel, unsigned int uiCLevel)
{
    // V = (2^L+1)*(2^C+1)
    unsigned int uiTwoPowLPlusOne = (1 << uiLevel) + 1;
    unsigned int uiTwoPowCPlusOne = (1 << uiCLevel) + 1;
    return uiTwoPowLPlusOne*uiTwoPowCPlusOne;
}
//----------------------------------------------------------------------------
unsigned int MgcBezierPatch::GetTrianglesPerTrianglePatch (
    unsigned int uiLevel)
{
    // T = 4^L
    return 1 << 2*uiLevel;
}
//----------------------------------------------------------------------------
unsigned int MgcBezierPatch::GetTrianglesPerRectanglePatch (
    unsigned int uiLevel)
{
    // T = 2*4^L
    return 2*(1 << 2*uiLevel);
}
//----------------------------------------------------------------------------
unsigned int MgcBezierPatch::GetTrianglesPerCylinderPatch (
    unsigned int uiLevel, unsigned int uiCLevel)
{
    // T = 2*2^L*2^C
    unsigned int uiTwoPowL = (1 << uiLevel);
    unsigned int uiTwoPowC = (1 << uiCLevel);
    return 2*uiTwoPowL*uiTwoPowC;
}
//----------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcBezierPatch::Factory (MgcStream& rkStream)
{
    // MgcBezierPatch is abstract, Factory never called
    return 0;
}
//---------------------------------------------------------------------------
void MgcBezierPatch::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_uiDegree);
    MgcStreamRead(rkStream,m_uiIndexQuantity);
    m_auiIndex = new unsigned int[m_uiIndexQuantity];
    MgcStreamRead(rkStream,m_auiIndex,m_uiIndexQuantity);
}
//---------------------------------------------------------------------------
void MgcBezierPatch::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcBezierPatch::Register (MgcStream& rkStream)
{
    return MgcObject::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcBezierPatch::Save (MgcStream& rkStream)
{
    MgcObject::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_uiDegree);
    MgcStreamWrite(rkStream,m_uiIndexQuantity);
    MgcStreamWrite(rkStream,m_auiIndex,m_uiIndexQuantity);
}
//---------------------------------------------------------------------------
